import 'package:f_base_one/model/Product.dart';
import 'package:flutter/material.dart';

class ProductItem extends StatelessWidget {
  final Product product;
  final Function(bool) onSelected;
  final Map<int, bool> selectedProducts;
  final Function(int) onUpdateQuantity;

  const ProductItem({
    super.key,
    required this.product,
    required this.onSelected,
    required this.onUpdateQuantity,
    required this.selectedProducts,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // Navigator.push(
        //   context,
        //   MaterialPageRoute(
        //     builder: (context) => Orderdetail(goodId: product.id),
        //   ),
        // );
        Navigator.pushNamed(
          context,
          '/orderdetail',
          arguments: {'productId': product.id, 'name': product.name},
        );
      },
      child: Container(
        padding: const EdgeInsets.all(12),
        margin: const EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey.shade300),
          borderRadius: BorderRadius.circular(10),
        ),
        child: Row(
          children: [
            Checkbox(
              value: selectedProducts[product.id] ?? false,
              onChanged: (bool? value) {
                if (value != null) {
                  onSelected(value);
                }
              },
            ),
            ClipRRect(
              borderRadius: const BorderRadius.all(Radius.circular(4)),
              child: Image.network(
                product.image,
                width: 100,
                height: 100,
                fit: BoxFit.cover,
              ),
            ),
            const SizedBox(width: 10),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Expanded(child: Text(product.name)),
                      const SizedBox(width: 10),
                      Text("X ${product.totalNum}"),
                    ],
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 10),
                    child: Container(
                      padding: const EdgeInsets.only(
                        left: 8,
                        right: 8,
                        top: 4,
                        bottom: 4,
                      ),
                      decoration: BoxDecoration(
                        color: const Color.fromARGB(255, 249, 247, 247),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Text(
                        product.attrsText,
                        style: const TextStyle(
                          fontSize: 12,
                          color: Colors.grey,
                        ),
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(child: Text("￥ ${product.curPrice}")),
                        Row(
                          children: [
                            IconButton(
                              onPressed: () {
                                if (product.totalNum > 1) {
                                  onUpdateQuantity(product.totalNum - 1);
                                }
                              },
                              icon: const Icon(Icons.remove),
                            ),
                            IconButton(
                              onPressed: () {
                                onUpdateQuantity(product.totalNum + 1);
                              },
                              icon: const Icon(Icons.add),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
